// pages/cardApply/cardApply.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    village_name: '',
    province: '',
    city: '',
    area: '',
    address: '', //详细地址
    full_name: '', //姓名
    tel: '', //电话号码
    IDcard: '', //身份证号码
    card_number: '', //卡号
    recharge_money: '', //充值金额
    other_money: '', //其它金额
    deposit: 0, //押金
    templateArr: [], //充值模板
    templateIndex: 999, //充值选择
    depositList: [], //押金列表
    depositIndex: 999, //押金选择
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getTemplate();
    this.getDeposit();
    this.getDefaultVillage();
  },



  // 选择园区
  choicelocation: function () {
    let _self = this;
    wx.navigateTo({
      url: '../choiceCommunity/choiceCommunity',
      events: {
        acceptDataFromOpenedPage: function (village) {
          let address = '';
          let province_name = village.province_name;
          let city_name = village.city_name;
          let area_name = village.area_name;
          if (province_name == city_name) {
            address = province_name + area_name + village.address;
          } else {
            address = province_name + city_name + area_name + village.address;
          }
          _self.setData({
            village_name: village.village_name,
            province: village.province,
            city: village.city,
            area: village.area,
            address: address
          })
        },
      }
    })
  },


  //获取园区默认地址
  getDefaultVillage: function () {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getDefaultVillage',
      data: {},
      success: function (res) {
        // console.log(res)
        if (res.data.code == 1000) {
          console.log(res.data)
          let address = '';
          let province_name = res.data.data.province_name;
          let city_name = res.data.data.city_name;
          let area_name = res.data.data.area_name;
          if (province_name == city_name) {
            address = province_name + area_name + res.data.data.address;
          } else {
            address = province_name + city_name + area_name + res.data.data.address;
          }
          that.setData({
            village_name: res.data.data.village_name,
            province: res.data.data.province,
            city: res.data.data.city,
            area: res.data.data.area,
            address: address
          })
        }
      }
    });
  },
  //提交
  formSubmit(e) {
    var that = this;
    let IDcard = e.detail.value.IDcard,
      address = e.detail.value.address,
      card_number = e.detail.value.card_number,
      depositIndex = e.detail.value.depositIndex,
      full_name = e.detail.value.full_name,
      other_money = e.detail.value.other_money,
      tel = e.detail.value.tel,
      templateIndex = e.detail.value.templateIndex;
    if (IDcard == '') {
      app.showToast('请输入证件号码', "none", 2000, function () {});
      return false;
    }
    if (that.data.village_name == "") {
      app.showToast('请选择园区', "none", 2000, function () {});
      return false;
    }
    if (card_number == '') {
      app.showToast('请输入或扫码水卡卡号', "none", 2000, function () {});
      return false;
    }
    if (full_name == '') {
      app.showToast('请输入姓名', "none", 2000, function () {});
      return false;
    }
    if (tel == '') {
      app.showToast('请输入电话', "none", 2000, function () {});
      return false;
    }
    if (templateIndex == 999) {
      app.showToast('请选择充值金额', "none", 2000, function () {});
      return false;
    }
    if (depositIndex == 999) {
      app.showToast('请选择押金', "none", 2000, function () {});
      return false;
    }
    if (that.data.village_name == '') {
      app.showToast('请选择园区', "none", 2000, function () {});
      return false;
    }
    let recharge_money = 0;
    let give_money = 0;
    let deposit = 0;

    if (templateIndex == 0) {
      if (other_money === '') {
        app.showToast('请输入其他充值金额', "none", 2000, function () {});
        return false;
      }
      if (other_money < 0) {
        app.showToast('请输入正确金额', "none", 2000, function () {});
        return false;
      }
      recharge_money = other_money;
    } else {

      recharge_money = that.data.templateArr[templateIndex].recharge;
      give_money = that.data.templateArr[templateIndex].give;
      console.log(recharge_money)
      console.log(give_money)
    }
    deposit = that.data.depositList[depositIndex];
    //console.log(recharge_money)
    app.ajax({
      url: 'Salesman/Card/entityCardApplication',
      data: {
        province: that.data.province,
        city: that.data.city,
        area: that.data.area,
        village_name: that.data.village_name,
        full_name: full_name,
        tel: tel,
        IDcard: IDcard,
        card_number: card_number,
        recharge_money: recharge_money,
        deposit: deposit,
        address: address
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "none", 2000, function () {});
          // that.setData({
          //   full_name: '',//姓名
          //   tel: '',//电话号码
          //   IDcard: '',//身份证号码
          //   card_number: '',//卡号
          //   recharge_money: '',//充值金额
          //   other_money: '',//其它金额
          //   deposit: 0,//押金
          //   templateIndex: 999,//充值选择
          //   depositIndex: 999,//押金选择
          // })
          wx.removeStorageSync('village')
          wx.navigateBack({
            delta: 1
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function () {});
        }

      }
    })
  },
  //获取充值模板
  getTemplate: function () {
    var that = this;
    app.ajax({
      url: 'Partner/Village/partnerRecharge',
      data: {},
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let templateArr = [];
          let template = res.data.data.template;
          template.money_0 = {};
          template.money_0.name = '其它金额(无赠送)';
          template.money_0.recharge = '';
          template.money_0.give = '';
          template.money_1.name = '充值' + template.money_1.recharge + '元赠送' + template.money_1.give + '元';
          template.money_2.name = '充值' + template.money_2.recharge + '元赠送' + template.money_2.give + '元';
          template.money_3.name = '充值' + template.money_3.recharge + '元赠送' + template.money_3.give + '元';
          template.money_4.name = '充值' + template.money_4.recharge + '元赠送' + template.money_4.give + '元';
          template.money_5.name = '充值' + template.money_5.recharge + '元赠送' + template.money_5.give + '元';
          template.money_6.name = '充值' + template.money_6.recharge + '元赠送' + template.money_6.give + '元';
          template.money_7.name = '充值' + template.money_7.recharge + '元赠送' + template.money_7.give + '元';

          templateArr.push(template.money_0)
          if (template.money_1.recharge != '') {
            templateArr.push(template.money_1)
          }
          if (template.money_2.recharge != '') {
            templateArr.push(template.money_2)
          }
          if (template.money_3.recharge != '') {
            templateArr.push(template.money_3)
          }
          if (template.money_4.recharge != '') {
            templateArr.push(template.money_4)
          }
          if (template.money_5.recharge != '') {
            templateArr.push(template.money_5)
          }
          if (template.money_6.recharge != '') {
            templateArr.push(template.money_6)
          }
          if (template.money_7.recharge != '') {
            templateArr.push(template.money_7)
          }
          that.setData({
            templateArr: templateArr
          })
        }
      }
    });
  },
  //充值选择
  templateChange(e) {
    this.setData({
      templateIndex: e.detail.value
    })
  },
  //获取押金列表
  getDeposit: function () {
    var that = this;
    app.ajax({
      url: 'Common/Common/cardDeposit',
      data: {},
      success: function (res) {
        console.log(res)
        if (res.data.code == 1000) {
          // let depositList = [];
          // depositList.push(res.data.data)
          that.setData({
            depositList: res.data.data
          })
        }
      }
    });
  },
  //押金选择
  depositChange(e) {
    this.setData({
      depositIndex: e.detail.value
    })
  },
  //扫描水卡
  scan: function () {
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        // this.setData({
        //   card_number: res.result
        // })
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },

  handleMoneyInput(e) {
    let money = app.retainDecimal(e.detail.value)
    this.setData({
      other_money: money
    })
  }
})